package com.example.noobpackage.ui.addpackage

import android.content.Context
import android.os.Bundle
import android.transition.TransitionInflater
import android.util.Log
import android.view.*
import android.view.inputmethod.InputMethodManager
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.example.noobpackage.R
import com.example.noobpackage.logic.model.ExpressData
import com.example.noobpackage.ui.MainViewModel
import kotlinx.android.synthetic.main.fragment_add_package.*
import kotlinx.android.synthetic.main.fragment_register.*

class AddPackageFragment : Fragment() {
    private val viewModel: AddPackageViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        sharedElementEnterTransition =
            TransitionInflater.from(context).inflateTransition(android.R.transition.move)
        setHasOptionsMenu(true)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_add_package, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val expressTypes = listOf("圆通", "中通", "申通", "韵达", "天天", "顺丰", "百世", "邮政")
        val typeAdapter = ArrayAdapter(requireActivity(), R.layout.select_input_item, expressTypes)
        (express_type_select.editText as AutoCompleteTextView).setAdapter(typeAdapter)

        val expressLocation = listOf("华工二饭菜鸟驿站", "天桥底丰巢", "天桥底快递点")
        val locationAdapter =
            ArrayAdapter(requireActivity(), R.layout.select_input_item, expressLocation)

        // 在此配置预置文本
        (express_location.editText as AutoCompleteTextView).setAdapter(locationAdapter)

        // 非空检测
        express_number_input.editText?.doOnTextChanged { text, _, _, _ ->
            if (text.toString() != "") express_number_input.error = null
        }
        express_type_select.editText?.doOnTextChanged { text, _, _, _ ->
            if (text.toString() != "") express_type_select.error = null
        }
        taking_express_number_input.editText?.doOnTextChanged { text, _, _, _ ->
            if (text.toString() != "") taking_express_number_input.error = null
        }
        express_location.editText?.doOnTextChanged { text, _, _, _ ->
            if (text.toString() != "") express_location.error = null
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            // 用户确认输入结束
            R.id.add_package_done -> {
                if (checkInput()) {
                    val number = express_number_input.editText?.text.toString()
                    val type = express_type_select.editText?.text.toString()
                    val takingCode = taking_express_number_input.editText?.text.toString()
                    val location = express_location.editText?.text.toString()
                    val express = ExpressData(number, type, takingCode, location)
                    Log.d("addTest", express.toString())

                    // 进行数据库存储并返回
                    viewModel.addExpress(express)
                    // 关闭输入法
                    (context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(
                        express_number_input.windowToken,
                        InputMethodManager.HIDE_NOT_ALWAYS
                    )
                    findNavController().navigateUp()
                }
            }
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.add_package_toolbar_menu, menu)
        super.onCreateOptionsMenu(menu, inflater)
    }

    private fun checkInput(): Boolean {
        var completed = true

        if (express_number_input.editText?.text.toString() == "") {
            express_number_input.error = "快递单号不能为空"
            completed = false
        } else {
            express_number_input.error = null
        }

        if (express_type_select.editText?.text.toString() == "") {
            express_type_select.error = "快递类型不能为空"
            completed = false
        } else {
            express_type_select.error = null
        }

        if (taking_express_number_input.editText?.text.toString() == "") {
            taking_express_number_input.error = "取件码不能为空"
            completed = false
        } else {
            taking_express_number_input.error = null
        }

        if (express_location.editText?.text.toString() == "") {
            express_location.error = "取件地点不能为空"
            completed = false
        } else {
            express_location.error = null
        }

        return completed
    }
}